package com.grechur.wanandroidcompose.page

import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.navigation.NavHostController
import com.grechur.wanandroidcompose.ui.theme.Purple200
import com.grechur.wanandroidcompose.ui.theme.Purple700
import okhttp3.internal.wait

@Composable
fun WebViewPage(globalNavHostController: NavHostController, url: String?, name: String?) {
    var progress by remember {
        mutableStateOf(0.1f)
    }
    val appBarHeight = 56.dp
    val statusBarDp = with(LocalDensity.current) {
        WindowInsets.statusBars.getTop(this).toDp()
    }
    val animatedProgress by animateFloatAsState(targetValue = progress,
        animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec)
    Scaffold(topBar = {
        TopAppBar(title = {
            Text(text = "$name", maxLines = 1, color = Color.White)
        }, backgroundColor = Color.Transparent,
            elevation = 0.dp,
            modifier = Modifier
                .background(Brush.linearGradient(listOf(
                    Purple700, Purple200
                )))
                .height(statusBarDp + appBarHeight)
                .padding(top = statusBarDp),
            navigationIcon = {
                IconButton(onClick = { globalNavHostController.popBackStack() }) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "返回", tint = Color.White)
                }
            })
    }) { padding ->
        Column(modifier = Modifier.padding(padding)) {
            LinearProgressIndicator(progress = animatedProgress)
            AndroidView(factory = { context ->
                val webView = WebView(context)
                webView.apply {
                    settings.javaScriptEnabled = true//支持javaScript脚本
                    webViewClient = object :
                        WebViewClient() {//创建webViewClient实例，作用是当网页内部跳转时在当前webView打开，而不是打开系统浏览器
                    }
                    loadUrl(url ?: "https://www.wanandroid.com")
                }
            }, modifier = Modifier.padding(padding),
                update = { webView ->
                    webView.webChromeClient = object : WebChromeClient() {
                        //监听网页加载进度
                        override fun onProgressChanged(view: WebView?, newProgress: Int) {
                            super.onProgressChanged(view, newProgress)
                            progress = newProgress.toFloat()
                        }
                    }
                })
        }

    }
}